//
//  PictureViewController.m
//  QT
//
//  Created by 甘世清 on 2024/10/23.
//

#import "PictureViewController.h"
#import "PictureDetailViewController.h"
#import "MyPictureListViewController.h"
#import <YCCardView/YCCardView.h>
#import "PicturePopView.h"
#import "MyPictureViewController.h"
#import "NoPictureView.h"
@interface PictureViewController ()<YCCardViewDelegate,YCCardViewDataSource>
@property (nonatomic , strong) YCCardView *cardView;
@property (nonatomic , strong) PictureModel * oneModel;
@property (nonatomic , strong) SQCustomButton *reloadBtn;
@property (nonatomic , strong) NoPictureView * noPictureView;
@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"图韵相易");
    self.view.backgroundColor = SQColorF1F1F1;
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"我的") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        MyPictureViewController * vc = [[MyPictureViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
}

- (void)SQ_UIViewForSelfView{
    
    [self.view addSubview:self.noPictureView];
    [self.noPictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    WS(weakSelf);
    [self.noPictureView.reloadBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_Header];
    }];
    [self.view addSubview:self.cardView];
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];

    [self.view addSubview:self.reloadBtn];
    [self.reloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
    }];
    [self.reloadBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
         [weakSelf SQ_Header];
    }];
    [self SQ_Header];
}
 
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:@"dynamic/randomOne" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        if (data.count > 0) {
            self.oneModel = [PictureModel mj_objectWithKeyValues:data];
            self.cardView.hidden = NO;
            [self.cardView yc_reloadDataWithAnimation:YES];
        } else {
            self.oneModel = nil;
            self.cardView.hidden = YES;
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"没有更多了T_T")];
        }
        self.noPictureView.hidden  = !self.cardView.hidden;
        self.reloadBtn.hidden = self.cardView.hidden;
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
    
}

/// 当前显示的卡片
/// @param cardView 父视图
/// @param cardCell 子视图
/// @param indexRow 当前索引
- (void)yc_cardDragView:(YCCardView *)cardView updateDisplayCell:(YCCardCell *)cardCell cellForRowAtIndex:(NSInteger)indexRow {
    PicturePopView *cell = (PicturePopView*)cardCell;
    cell.model = self.oneModel; //[self.jsonArray objectAtIndex:indexRow];
}

 
/// cell点击事件
/// @param cardView 父视图
/// @param indexRow 索引
- (void)yc_cardView:(YCCardView *)cardView didSelectedIndex:(NSUInteger)indexRow {
    PictureDetailViewController * vc = [[PictureDetailViewController alloc]init];
    vc.model = self.oneModel; //[self.jsonArray objectAtIndex:indexRow];
    [self SQ_pushViewControllerFull:vc];
}

/**
 卡片是否可以移动，拖拽的时候，可以进行一些条件控制，比如喜欢次数用完了，需要用户充值vip等
 * @param cardView 卡片拖拽视图
 * @param isRightDirection 是否为右边
 */
- (BOOL)yc_cardDragView:(YCCardView *)cardView cheackCardHasCanDragWithDragingDirection:(BOOL )isRightDirection {
  
    return NO;
}
/**
 卡片拖拽完成代理
 * @param cardView 卡片拖拽视图
 * @param cardIndex 卡片索引位置
 * @param isRight 是否向右侧滑动
 */
- (void)yc_cardDragViewFinish:(YCCardView *)cardView cardCellIndex:(NSInteger )cardIndex direction:(BOOL )isRight{
//    NSObject * model = [self.jsonArray objectAtIndex:cardIndex];
  
}


#pragma mark - YCCardViewDataSource

- (NSInteger)yc_numberOfRowsInCardDragView:(YCCardView *)cardView {
    if (self.oneModel) {
        return 1;
    }
    return 0;
}

- (UIEdgeInsets)yc_edgeInsetsInCardDragView:(YCCardView *)cardView {
    return UIEdgeInsetsMake(TopHeight+10,20, FootHeight+160, 20);
}

- (YCCardCell *)yc_creatCellForCardView:(YCCardView *)cardView {
    PicturePopView *cell =  [[PicturePopView alloc]init];
    return cell;
}


/**
 加载更多数据
 */
- (void)yc_loadDataMore {
    
}

 

- (YCCardView *)cardView{
    if (!_cardView) {
        _cardView = [[YCCardView alloc]initWithFrame:self.view.frame];
        _cardView.delegate = self;
        _cardView.dataSource = self;
    }
    return _cardView;
}
- (SQCustomButton *)reloadBtn{
    if (!_reloadBtn) {
        _reloadBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(18, 18) midmargin:10];
        _reloadBtn.text = SQNSLocalString(@"刷新");
        _reloadBtn.image = SQGetImage(@"xiayige");
        _reloadBtn.font = FONT_MEDIUM(14);
        _reloadBtn.textColor = SQColor333333;
        _reloadBtn.layer.cornerRadius = 10;
        _reloadBtn.layer.borderColor = SQColor333333.CGColor;
        _reloadBtn.layer.borderWidth = 0.5;
        _reloadBtn.backgroundColor = UIColor.whiteColor;
    }
    return _reloadBtn;
}
- (NoPictureView *)noPictureView{
    if (!_noPictureView) {
        _noPictureView = [[NoPictureView alloc]init];
        _noPictureView.hidden = YES;
    }
    return _noPictureView;
}
@end
